package com.websuites.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil 
{
	 public static Date toDate(String sDate, String sFmt) 
	 {
		 Date dt = null;
	     try 
	     {
	    	 dt = new SimpleDateFormat(sFmt).parse(sDate);
	     } 
	     catch (ParseException e) 
	     {
	    	 return dt;
	     }
	     return dt;
	 }
	 
	 public static String getTime() 
	 {
		 return new SimpleDateFormat("yyyy-MM-dd HH:MM:SS").format(new Date());	 
	 }

	 public static String getTime(String sFmt) 
	 {
		 return new SimpleDateFormat(sFmt).format(new Date());	 
	 }

	 public static long daysBetween(String sFrom, String sEnd)
	 {
		 Date dtFrom = null;
	     Date dtEnd = null;
	     dtFrom = toDate(sFrom, "yyyyMMdd");
	     dtEnd  = toDate(sEnd, "yyyyMMdd");
	     long begin = dtFrom.getTime();
	     long end = dtEnd.getTime();
	     long inter = end - begin;
	     if (inter < 0) 
	     {
	    	 inter = inter * (-1);
	     }
	     
	     long dateMillSec = 24 * 60 * 60 * 1000;	        
	     long dateCnt =  inter / dateMillSec;	        
	     long remainder = inter % dateMillSec;
	        
	     if (remainder != 0) 
	     {
	    	 dateCnt++;
	     }
	     return dateCnt;
	 }
	 
	 public static long daysBetween(Date dtFrom, Date dtEnd)
	 {
	     long begin = dtFrom.getTime();
	     long end = dtEnd.getTime();
	     long inter = end - begin;
	     if (inter < 0) 
	     {
	    	 inter = inter * (-1);
	     }
	     
	     long dateMillSec = 24 * 60 * 60 * 1000;	        
	     long dateCnt =  inter / dateMillSec;	        
	     long remainder = inter % dateMillSec;
	        
	     if (remainder != 0) 
	     {
	    	 dateCnt++;
	     }
	     return dateCnt;
	 }

	 public static long daysBetween(String sFrom, Date dtEnd)
	 {
		 Date dtFrom = null;
	     dtFrom = toDate(sFrom, "yyyyMMdd");

	     long begin = dtFrom.getTime();
	     long end = dtEnd.getTime();
	     long inter = end - begin;
	     if (inter < 0) 
	     {
	    	 inter = inter * (-1);
	     }
	     
	     long dateMillSec = 24 * 60 * 60 * 1000;	        
	     long dateCnt =  inter / dateMillSec;	        
	     long remainder = inter % dateMillSec;
	        
	     if (remainder != 0) 
	     {
	    	 dateCnt++;
	     }
	     return dateCnt;
	 }
	 
}
